#include "StdAfx.h"
#include "GameStateLv1.h"
#include "cSound.h"
extern CMySprite* pbmTiles;
extern int jumpPos;
extern bool isJump;
extern bool canRight;
extern bool canLeft;
extern bool sound;
extern bool life;

CGameStateLv5::CGameStateLv5(void)
{
}

CGameStateLv5::~CGameStateLv5(void)
{
}
CGameStateLv5::CGameStateLv5(CGameMain *pGameMain) {
	pGM = pGameMain;
	GameStateID = STATE_LV5;
	isLeft = false;
	canJump = true;
	monsStep = 10;
}

void CGameStateLv5::MessageEnter(HDC* pDC)
{
	if (sound)
		PlaySound(L"res\\Music\\beginlevel.wav",NULL, SND_FILENAME | SND_ASYNC);

	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster02.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster01.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster03.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster09.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster05.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster05.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster02.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster01.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster03.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster09.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster05.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster05.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster03.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster09.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster05.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster07.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster04.png"));
	monsters.push_back((CMonster*)new CMySprite(L"res\\Monster08.png"));
	for(int i=0; i< monsters.size(); ++i) 
		monsters[i]->GetRect(4,3);

	Mario				= new CMyMario();
	sprMap				= new CMyMap();

	Mario->GetRect(7,5);				// Get rect of png
	Mario->RenderPosition(144,336);	// Set the position of character	
	LoadMap();
	bmBkg = new Bitmap(L"res\\Background\\5.jpg"); // GameStateLv1 Background
	
}
void CGameStateLv5::LoadMap() {
	ifstream Map("res\\Level5\\Map1.dat");
	string tmp;
	if ( Map.is_open() ) {
		while ( !Map.eof() ) {
			getline (Map,tmp);
			mapMatrix.push_back(tmp);
		}
	}
	Map.close();

	int frPos = 0;
	int posX = 0;
	int posY = 0;
	int count = 0;
	vector<Point> monstersPos;
	vector<Point> moneysPos;

	for (int i = 0; i< mapMatrix.size(); ++i)
		for (int j =0; j< mapMatrix[i].size(); ++j) {
			frPos	= mapMatrix[i][j] - 65;
			posX	= i * pbmTiles->rectX;
			posY	= j * pbmTiles->rectY;
			if (mapMatrix[i][j] == 'm')
				monstersPos.push_back(Point(posX,posY));
			if (mapMatrix[i][j] == 'H') {
				moneys.push_back(new CMoney());
				count++;
				moneysPos.push_back(Point(posX,posY));
				pbmTiles->RenderPosition(posY,posX);
				pbmTiles->Render(sprMap->bm,23);
			}
			else {
				pbmTiles->RenderPosition(posY,posX);
				pbmTiles->Render(sprMap->bm,frPos);
			}
		}
		for (int i=0;i<monstersPos.size();++i)
			monsters[i]->RenderPosition(monstersPos[i].Y,monstersPos[i].X-18);
		for (int i=0;i<moneysPos.size();++i)
			moneys[i]->RenderPosition(moneysPos[i].Y,moneysPos[i].X);
		for(int i=0; i< moneys.size(); ++i) // Get rect for moneys 
			moneys[i]->GetRect(3,3);
}
void CGameStateLv5::MessagePaint(HDC *pDC)
{
	// Draw Background
	Graphics g(*pDC);
	g.DrawImage(bmBkg,0,0);

	sprMap->Draw(pDC);
	for(int i=0;i<monsters.size();++i)
		monsters[i]->RenderAnimation(pDC);
	for(int i=0;i<moneys.size();++i)
		moneys[i]->RenderAnimation(pDC);
	Mario->RenderAnimation(pDC);
}

void CGameStateLv5::MessageUpdate(int iKey)
{
	int rcT = pbmTiles->rectX;
	int topX = Mario->currX / rcT ; 
	int topY = Mario->currY / rcT + 1 ; 
	if (mapMatrix[topY-1][topX] != '?' || topY-1 < 0) { // Check TOP
		isJump = false;
	}

	// Clear Monster
	for (int i=0;i<monsters.size();++i) {
		if (monsters[i]->frPos == 3)
			monsters.erase(monsters.begin()+i);
	}
	// Clear Money
	for (int i=0;i<moneys.size();++i) {
		if (moneys[i]->lose)
			moneys.erase(moneys.begin()+i);
	}
	// Move monsters
	for (int i=0;i<monsters.size();++i)
		monsters[i]->NextFrame();
	for (int i=0;i<moneys.size();++i)
		moneys[i]->NextFrame();

	if ((iKey & RIGHT) == RIGHT){ 
		if (isLeft) {
			Mario->bmSprite->RotateFlip(Rotate180FlipY);
			isLeft = false;
		}
		Mario->NextFrame();
		if (canRight) {
			if (canJump && !isJump && sound)
				PlaySound(L"res\\Music\\step.wav",NULL, SND_FILENAME | SND_ASYNC);
			Mario->currX += DISTANCE;
			sprMap->ToRight();
			MoveThings(-DISTANCE);
		}
	}
	else if ((iKey & LEFT)	== LEFT) {
		if (!isLeft) {
			Mario->bmSprite->RotateFlip(Rotate180FlipY);
			isLeft = true;
		}
		Mario->PrevFrame();
		if (canLeft) {
			if (canJump && !isJump && sound)
				PlaySound(L"res\\Music\\step.wav",NULL, SND_FILENAME | SND_ASYNC);
			if (Mario->currX - DISTANCE < 12)
				canLeft = false;
			else {
				Mario->currX -= DISTANCE;
				sprMap->ToLeft();
				MoveThings(DISTANCE);
			}
		}
	}

	else if ( (iKey & UP) == UP && !isJump && canJump) {
		if (sound)
			PlaySound(L"res\\Music\\jump.wav",NULL, SND_FILENAME | SND_ASYNC);
		jumpPos	= 0;
		isJump	= true;
	}

	// Check if Mario is jumping
	if (isJump) {
		int pY = Mario->currY - JUMP;
		if (jumpPos >= LJUMP || pY < 24)
			isJump = false;
		else {
			jumpPos += JUMP;
			Mario->RenderPosition(-1,pY);
		}
		
	}

	/*********************TUONG TAC*************************/
	int x = (Mario->currX+Mario->rectX) / rcT ; 
	int y = (Mario->currY+Mario->rectX) / rcT + 1; 

	if (canRight && isJump && mapMatrix[topY-1][x+1] !='?' && (iKey & RIGHT) == RIGHT ) {			// Kiem tra nhay cheo phai
		isJump =false;
	}
	if (canRight && isJump && mapMatrix[topY-1][x-1] !='?' && (iKey & LEFT)	== LEFT ) {				// Kiem tra nhay cheo phai
		isJump =false;
	}
	// Check RIGHT
	if (mapMatrix[y][x+1] != '?' && Mario->currX == x*rcT ) {									
		if (mapMatrix[y][x+1] == 'm')
			Mario->die = true;
		canRight = false;
	}
	else
		canRight = true;
	// Check LEFT 
	if (mapMatrix[y][x-1] != '?' && Mario->currX == x*rcT) { 
		if (mapMatrix[y][x-1] == 'm')
			Mario->die = true;
		canLeft = false;
	}
	else
		canLeft = true;
	 //Check BOTTOM
	if (y+1 < 20 && mapMatrix[y+1][x] != '?' && Mario->currY == (y-1)*rcT ) {  
		if (mapMatrix[y+1][x] == 'm') { 
			// Neu duoi chan la quai vat thi tim quai vat co toa do tuong ung va xoa hinh quai vat va vi tri quai vat tren ma tran.
			for(int i=0;i<monsters.size();++i)
				if (monsters[i]->currY / 24 == y)
					monsters[i]->Die();
			mapMatrix[y+1][x] = '?';
		}

		canJump = true;
	}
	else if (!isJump && y+1<20) {
		int pY = Mario->currY + GRAVITY;
		Mario->RenderPosition(-1,pY);
		canJump = false;
	}

	if ( y == 19 && sound) {
		Mario->die = true;
		//restart 
	}

	HandleMoney(); // Kiem tra neu mario an tien
	MarioDead(); // Kiem tra truong hop Mario chet
	Complete(); // Kiem tra mario gap ong gia qua ham

	if ((iKey & KEY_N) == KEY_N) {
		pGM->SwitchState(STATE_WIN);
	}

}
void CGameStateLv5::Reset() {
	MessageExit();
	MessageEnter(NULL);
}

// Neu nhan vat Mario nam trong dong tien thi tien mat
// vi tri dong tien thanh trong tren ma tran
// va play sound neu option co sound
void CGameStateLv5::HandleMoney() {
	int marioX = Mario->currX;
	int marioY = Mario->currY;
	for (int i=0;i<moneys.size();++i) {
		if ( 
			( marioX >= moneys[i]->currX && marioX <= moneys[i]->currX + 24 )
			&& (marioY >= moneys[i]->currY && marioY <= moneys[i]->currY + 24) 
			) {
				moneys[i]->Lose();	
				if (sound)
					PlaySound(L"res\\Music\\coin.wav",NULL, SND_FILENAME | SND_ASYNC);
				mapMatrix[moneys[i]->currY / 24][moneys[i]->currX / 24] = '?';
				break;
		   }
	}
}
// Kiem tra Mario chet, neu con mang thi het chet
// if mario chet > tru mang, kiem tra mang, neu con mang render lai va mario chet la false
void CGameStateLv5::MarioDead() {
	if (Mario->die) {
		--Mario->life;
		if (Mario->life <= 0) {
			pGM->SwitchState(STATE_LOSE);
		}
		else {
			if (isLeft) {
				Mario->bmSprite->RotateFlip(Rotate180FlipY);
				isLeft = false;
			}
			Reset();
			Mario->die = false;
		}
	}

}
void CGameStateLv5::MessageExit() {
	mapMatrix.clear();
	monsters.clear();
	moneysPos.clear();
	monstersPos.clear();

	delete Mario;
	delete sprMap;
	delete bmBkg;
}
void CGameStateLv5::MoveThings(int distance) {
	for(int i=0;i<monsters.size();++i) 
		monsters[i]->px += distance;
	for(int i=0;i<moneys.size();++i)
		moneys[i]->px +=distance;
}
// Kiem tra coi co gap ong gia qua ham chua
void CGameStateLv5::Complete() {	
	int marioX = Mario->currX;
	int marioY = Mario->currY;
		if ( mapMatrix[marioY / 24][marioX / 24 + 1] == 'O' 
			|| mapMatrix[marioY / 24][marioX / 24 - 1] == 'O'
			|| mapMatrix[marioY / 24 +1][marioX / 24] == 'O'
			|| mapMatrix[marioY / 24 -1][marioX / 24] == 'O')
			pGM->SwitchState(STATE_WIN);
}